<template>
  <!-- 退出功能实现原理
    基于token的方式实现退出比较简单，只需要销毁本地的token即可。这样，
    后续的请求就不会携带token，必须重新登录生成一个新的token之后才可以访问页面。
     -->
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header height="70px">
      <div>
        <img src="../assets/img/logo.jpg" alt="" />
        <span>电商管理后台</span>
      </div>
      <el-button type="warning" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主题区域 -->
    <el-container>
      <!-- 侧边栏 -->

      <el-aside :width="isToggleCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="getCollapse">|||</div>
        <!-- 菜单栏区域 -->
        <el-menu
          background-color="#c7d5d6"
          text-color="#000"
          active-text-color="#da9255"
          unique-opened
          :collapse="isToggleCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <el-submenu
            v-for="Item in menuList"
            :key="Item.id"
            :index="Item.id + ''"
          >
            <!-- 一级菜单区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconObj[Item.id]"></i>
              <!-- 文字 -->
              <span>{{ Item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in Item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <!-- 图标 -->
              <i class="el-icon-menu"></i>
              <!-- 文字 -->
              <span>{{ subItem.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>

import $ from 'jquery'
// $('.toggle-button').click(function(){
//   this.style.display = none;
//   // 初始状态是200px 点击一次向主内容区域添加magin-left
//   $('.el-main').toggleClass("marginLeft_64")
// })


export default {
  name: "home",
  data() {
    return {
      // 存储所有菜单的列表
      menuList: [],
      // icon图标对象
      iconObj: {
        125: "iconfont icon-users",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
      isToggleCollapse: false,
      // 当前最活跃的路径
      activePath:''
    };
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem('activePath');
  },
  methods: {
    logout() {
      // 清空token
      window.sessionStorage.clear();
      // 跳转到登录页面
      this.$router.push("/login");
    },
    // 获取所有的菜单
    async getMenuList() {
      const { data: res } = await this.$axios.get("menus");
      if (res.meta.status !== 200)
        return this.$message.error("获取结果失败！！！");
      // this.$message.success("成功获取！！！！");
      this.menuList = res.data;
      // console.log(this.menuList);
    },
    // 左侧菜单栏的折叠与展开
    getCollapse() {
      this.isToggleCollapse = !this.isToggleCollapse;
      if(this.isToggleCollapse == true){
      $('.el-main').css("margin-left", "64px")
      }else{
        $('.el-main').css("margin-left", "200px")
      }

    },
    // 将当前最活跃的路径存到缓存中
    saveNavState(activePath){
      window.sessionStorage.setItem('activePath',activePath);
      this.activePath = activePath;
    }
  },
};
</script>

<style scoped>
.home-container {
  height: 100%;
}
.el-header {
  /* 固定定位 */
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1000;
  background-color: #9cbbb3;
  display: flex;
  justify-content: space-between;
  align-items: center;
  /* padding-left: 0; */
}
.el-header div {
  display: flex;
  align-items: center;
  font-size: 20px;
}
.el-header img {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  box-shadow: 0 0 10px #6ca5b3;
  margin-right: 10px;
}

.el-aside {
  position: fixed;
  top:0;
  left: 0;
  right: 0;
  bottom: 0;
  margin-top: 70px;
  background-color: #c7d5d6;
  z-index: 1000;
}
.el-aside .el-menu {
  border-right: 0;
}
.el-aside .toggle-button {
  height: 28px;
  background-color: #e0b583;
  font-size: 10px;
  line-height: 28px;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
.iconfont {
  margin-right: 8px;
}
.el-main {
  margin-top: 70px;
  background-color: #f9efe6;
  margin-left: 200px;
}
</style>